package com.hankol.newretail

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.BaseConstant
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.baselibrary.activity.BaseActivity
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.newretail.adapter.details.GoodsAppraiseAdapter
import com.hankol.newretail.databinding.ActivityAllAppraiseBinding
import com.hankol.newretail.viewmodel.EvaluationViewModel
import com.hankol.newretail.viewmodel.GoodsDetailViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import java.util.HashMap

/**
 *@author:hanke
 *2022/9/5
 *desc:
 */
@Route(path = ActivityRouter.NEW_RETAIL.ALL_APPRAISE)
class AllAppraiseActivity : BaseActivity() {
    private var page: Int = 1
    private var spuId = 0;
    private lateinit var dataBinding: ActivityAllAppraiseBinding
    private val mAdapter: GoodsAppraiseAdapter = GoodsAppraiseAdapter()
    private lateinit var viewModel: GoodsDetailViewModel
    override fun getLayoutId(): Int {
        return R.layout.activity_all_appraise
    }

    override fun init() {
        if (ARouterUtils.parseParams(intent.extras!!, "spuId")[0].checkNotNull()) {
            spuId = ARouterUtils.parseParams(intent.extras!!, "spuId")[0].toInt()
        }
        dataBinding = getDataBinding<ActivityAllAppraiseBinding>()
        viewModel = getActivityViewModelProvider(this).get(GoodsDetailViewModel::class.java)
        initView()
        initObserver()
        requestData()
    }

    private fun initObserver() {
        viewModel.goodsEvaluationLD.observe(this, Observer { it ->
            dataBinding.srlAllAppraise.finishRefresh()
            dataBinding.srlAllAppraise.finishLoadMore()
            if (it.data.checkNotNull()) {
                if (page == 1) {
                    mAdapter.setNewData(it.data)
                } else {
                    mAdapter.addData(it.data)
                }
                page++
            } else {
                if (page == 1) {
                    mAdapter.setNewData(arrayListOf())
                    val emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_ORDER_LIST, dataBinding.rvAllAppraise, "暂无评价")
                    mAdapter.emptyView = emptyView
                }
            }
        })
    }

    private fun requestData() {
        viewModel.reqGoodsEvaluation(spuId.toLong(), page)
    }

    private fun initView() {
        dataBinding.ivBack.setOnClickListener { finish() }
        mAdapter.onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.civ_avatar, R.id.tv_name -> goUserHomePage(mAdapter.data[position].user.id)
                R.id.civ_shop_avatar, R.id.tv_shop_name -> goUserHomePage(mAdapter.data[position].ownerReplyList[0].user.id)
            }
        }
        dataBinding.srlAllAppraise.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                requestData()

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                requestData()
            }
        })
        mAdapter.setShowReplay(true)
        dataBinding.rvAllAppraise.adapter = mAdapter
    }

    private fun goUserHomePage(userId: Int) {
        val hashMap = HashMap<String, Any>()
        hashMap[BaseConstant.USER_ID] = userId
        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap)
    }

}